#obj plugin v1.6b
def inf(support,im_types,ex_types,C_mthds): #plugin information
    ex_types.append(('Wavefront','*.obj')) #individual export type
    C_mthds.append(['obj','Wavefront','im_obj','ex_obj'])
    #GUI = 'ex'

import GlobalTMP as G
from Tkinter import *
"""
def exgui():
        apply_transforms = 0
        
        gui = GUI()
        gui.master.geometry("200x256")
        gui.master.maxsize (width=200, height=256)
        gui.master.minsize (width=200, height=256)
        gui.master.title("export OBJ")
        
        gui.mainloop()
"""
def impt(imfl,imSC,debug): #import function
    pass #we don't use this function (export only)

def expt(exfl,exSC,debug): #export function
    if exfl.split(".")[-1] == "obj" and exSC == 'Wavefront': #check extension + safety check (requitred)
        fw = open(exfl,'w') #open obj file
        G.setRead()
        G.getSpecs()
        
        vcnt,ncnt,ucnt = 0,0,0
        apply_transforms = 0
        
        if G.vecs_global:
            if G.vecs_transf:# and apply_transforms == 1:
                for v in G.applyTransforms(G.getVector(0,0,'global',0),G.getVector(0,1,'global',0)): fw.write('v '+' '.join(v)+'\n')
                for n in G.applyTransforms(G.getVector(1,0,'global',0),G.getVector(1,1,'global',0)): fw.write('vn '+' '.join(n)+'\n')
                for u in G.applyTransforms(G.getVector(2,0,'global',0),G.getVector(2,1,'global',0)): fw.write('vt '+' '.join(u)+'\n')
            else:
                for v in G.getVector(0,0,'global',0): fw.write('v '+' '.join(v)+'\n')
                for n in G.getVector(1,0,'global',0): fw.write('vn '+' '.join(n)+'\n')
                for u in G.getVector(2,0,'global',0): fw.write('vt '+' '.join(u)+'\n')

        PList = G.getObjectIDs()

        for P in PList:
            
            if not G.vecs_global:
                if G.vecs_transf:# and apply_transforms == 1:
                    for v in G.applyTransforms(G.getVector(0,0,str(P),0),G.getVector(0,1,str(P),0)): fw.write('v '+' '.join(v)+'\n')
                    for n in G.applyTransforms(G.getVector(1,0,str(P),0),G.getVector(1,1,str(P),0)): fw.write('vn '+' '.join(n)+'\n')
                    for u in G.applyTransforms(G.getVector(2,0,str(P),0),G.getVector(2,1,str(P),0)): fw.write('vt '+' '.join(u)+'\n')
                else:
                    for v in G.getVector(0,0,str(P),0): fw.write('v '+' '.join(v)+'\n')
                    for n in G.getVector(1,0,str(P),0): fw.write('vn '+' '.join(n)+'\n')
                    for u in G.getVector(2,0,str(P),0): fw.write('vt '+' '.join(u)+'\n')

            fw.write('o object '+str(P)+'\n')
            TList = (G.convertToTriangles(G.getPrimitives(P),0))
            for Triangles in TList:
                FPs = Triangles[1]
                        
                index = 0
                while index != len(FPs):
                    P1 = FPs[index]
                    P2 = FPs[index+1]
                    P3 = FPs[index+2]
                    fw.write('f ')
                    if P1[0] != '':
                        fw.write(str(int(P1[0])+vcnt+1))
                        if P1[2] != '':
                            fw.write('/'+str(int(P1[2])+ucnt+1))
                            if P1[1] != '':
                                fw.write('/'+str(int(P1[1])+ncnt+1))
                    fw.write(' ')
                    if P2[0] != '':
                        fw.write(str(int(P2[0])+vcnt+1))
                        if P2[2] != '':
                            fw.write('/'+str(int(P2[2])+ucnt+1))
                            if P2[1] != '':
                                fw.write('/'+str(int(P2[1])+ncnt+1))
                    fw.write(' ')
                    if P3[0] != '':
                        fw.write(str(int(P3[0])+vcnt+1))
                        if P3[2] != '':
                            fw.write('/'+str(int(P3[2])+ucnt+1))
                            if P3[1] != '':
                                fw.write('/'+str(int(P3[1])+ncnt+1))
                    fw.write('\n')
                    index += 3

            if not G.vecs_global:
                vcnt += len(G.getVector(0,0,str(P),0))
                ncnt += len(G.getVector(1,0,str(P),0))
                ucnt += len(G.getVector(2,0,str(P),0))
        
        G.closeRead()
        fw.close()
